﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Blog;
using Entity.Blog.Dto;
using Entity.Blog.Dto.BlogCategory;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Blog
{
    public class BlogCategoryService
    { 
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public BlogCategoryService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<BlogCategory>>> GetList(BlogCategorySearchDto model)
        {
            //筛选
            var query = _context.BlogCategories
                .WhereIF(a => a.Pid == model.Pid, model.Pid != null)
                .WhereIF(a => a.Name.Contains(model.Name), !string.IsNullOrEmpty(model.Name))
                ;

            //返回列表
            var list = await query
                .OrderBy(a => a.Order)
                .Page(model.PageIndex, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<BlogCategory>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增
         */
        public async Task<RequestResponseModel<string>> Add(BlogCategoryAddDto model)
        {
            //新增数据
            var entity = _mapper.Map<BlogCategory>(model);

            //更新表
            _context.BlogCategories.Add(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        }

        /**
         * 更新
         */
        public async Task<RequestResponseModel<string>> Put(BlogCategoryUpdateDto model)
        {
            //校验数据
            var query = await Valid.ValidId<BlogCategory>(_context.BlogCategories, model.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }

            //编辑数据
            var entity = _mapper.Map(model, query);

            //更新表
            _context.BlogCategories.Update(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", entity.Id);
        }

        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> Delete(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<BlogCategory>(_context.BlogCategories, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.BlogCategories.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
